import appStore from "@/stores"

// 系统监听变量
let matchMedia: any = ""
const watchSystemThemeChange = () => {
    if (matchMedia) return
    matchMedia = window.matchMedia("(prefers-color-scheme: dark)")
    matchMedia.onchange = () => {
        changeTheme('system')
    }
}
/**
 * 变更主题
 * @param theme
 */
const changeTheme = (theme: string) => {
    let themeClassName = ""
    switch (theme) {
        case 'light':
            themeClassName = "light"
            break
        case 'dark':
            themeClassName = "dark"
            break
        case 'system':
            // 调用方法监听系统主题变化
            watchSystemThemeChange()
            themeClassName = matchMedia.matches ? "dark" : "light"
            break
    }
    // 修改 html中class
    const html = document.querySelector("html")
    if (html) {
        html.className = themeClassName
    }
}

